<?php

namespace app\admin\controller;

use app\common\controller\Adminbase;
use think\Request;
use think\facade\Config;

class Auth extends Adminbase
{
    protected $add_edit_field;

    public function initialize(){
        parent::initialize();
        $menu = db('menu')->select();
        $e_a_i = [];
        $e_a_i += [0=>'顶级节点'];
        foreach ($menu as $k=>$v){
            $e_a_i[$v['id']] = $v['name'];
        }
        $this->add_edit_field = [
            ['field'=>'name','title'=>'节点名称','type'=>'text','verify'=>'required','tips'=>''],
            ['field'=>'pid','title'=>'父节点','type'=>'select','verify'=>'required','tips'=>'','data'=>$e_a_i,'value'=>'0'],
            ['field'=>'conditions','title'=>'节点地址','type'=>'text','tips'=>'填写格式 模块名/控制器/方法 小写/可为空'],
            ['field'=>'icon','title'=>'节点图标','type'=>'text','tips'=>'图标格式：layui-icon layui-icon-face-surprised 具体图标参考Layui','value'=>'layui-icon '],
            ['field'=>'type','title'=>'鉴权方式','type'=>'select','tips'=>'节点权限鉴别方式','data'=>config("otherconfig.admin_auth.type"),'value'=>'1'],
            ['field'=>'is_menu','title'=>'是否菜单显示','type'=>'radio','tips'=>'','data'=>config("otherconfig.admin_auth.is_menu"),'value'=>'2'],
            ['field'=>'log_type','title'=>'日志记录','type'=>'select','verify'=>'required','tips'=>'debug说明：会一并记录提交的参数','data'=>config("otherconfig.admin_auth.log_type"),'value'=>'2'],
            ['field'=>'status','title'=>'节点状态','type'=>'radio','verify'=>'required','tips'=>'','data'=>['1'=>'正常','2'=>'禁用'],'value'=>'1'],
            ['field'=>'order_by','title'=>'节点排序','type'=>'number','verify'=>'number','tips'=>'排序，值越小越靠前','value'=>'1']
        ];
    }
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $info = db('menu')->order('id asc')->select();
        $Preprocessing = Config::get('otherconfig.admin_auth');
        if($info){
            foreach ($Preprocessing as $k=>$v){
                foreach ($info as $k1=>$v1){
                    if(isset($v1[$k])){
                        $info[$k1][$k] = $v[$v1[$k]];
                    }
                }
            }
            $info = array_recursion($info);
        }
        $filed = [
            ['field'=>'id','title'=>'ID'],
            ['field'=>'name','title'=>'节点名称'],
            ['field'=>'conditions','title'=>'节点地址'],
            ['field'=>'type','title'=>'鉴权方式'],
            ['field'=>'is_menu','title'=>'是否菜单'],
            ['field'=>'log_type','title'=>'日志记录方式'],
            ['field'=>'status','title'=>'节点状态'],
            ['field'=>'order_by','title'=>'节点排序'],
            ['field'=>'create_at','title'=>'添加时间'],
            ['field'=>'update_at','title'=>'更新时间'],
        ];
        return view('public/list',['filed'=>$filed,'info'=>json_encode($info,JSON_UNESCAPED_UNICODE),'table'=>'tree_table']);
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function save()
    {
        return view('public/add',['filed'=>$this->add_edit_field]);
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function add(Request $request)
    {
        $data = $request->param();
        if(!$data || !is_array($data)){
            return json(['code'=>1,'msg'=>'数据格式错误，need Array']);
        }
        $res = $this->add_info('menu',$data);
        return json($res);
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param \think\Request $request
     * @return \think\Response
     */
    public function edit(Request $request)
    {
        $id = $request->param('id');
        if(!$id){
            return json(['code'=>1,'msg'=>'数据错误，need ID']);
        }
        $data = db('menu')->where('id',$id)->find();
        if(!$data){
            return json(['code'=>1,'msg'=>'数据不存在']);
        }
        return view('public/edit',['filed'=>$this->add_edit_field,'info'=>$data]);
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function update(Request $request)
    {
        $data = $request->param();
        if(!$data || !is_array($data)){
            return json(['code'=>1,'msg'=>'数据格式错误，need Array']);
        }
        $res = $this->update_info('menu',$data);
        return json($res);
    }

    /**
     * 删除指定资源
     *
     * @param \think\Request $request
     * @return \think\Response
     */
    public function delete(Request $request)
    {
        $id = $request -> post('id');
        if(!is_array($id)){
            if(!is_numeric($id)){
                return json(['code'=>1,'msg'=>'数据错误']);
            }else{
                $id = explode(',',$id);
            }
        }
        if(is_array($id)){
            foreach($id as $val){
                $old = db('menu')->where('id',$val)->find();
                if(!$old){
                    return json(['code'=>1,'msg'=>'存在非数据库数据，请核查']);
                }
            }
            $res = db('menu')->delete($id);
            if($res){
                return json(['code'=>200,'msg'=>'删除成功']);
            }else{
                return json(['code'=>1,'msg'=>'删除失败']);
            }
        }else{
            return json(['code'=>1,'msg'=>'数据错误']);
        }
    }
}
